<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/01/0001
 * Time: 11:42
 */

namespace app\sports_admin\model;


use think\Model;

class Contents extends Model
{
    protected $table = 'table_scoring';

    protected $pk = 'id';

    protected $createTime = 'add_time';

    protected $updateTime = 'update_time';

    protected $autoWriteTimestamp = true;

    public function getAddTimeAttr($value)
    {
        return date('Y-m-d H:i:s',$value);
    }

    public function getSexAttr($value)
    {
        return $value == 1 ? '男' : '女';
    }
    public function project()
    {
        return $this->belongsTo('sports_project','project_id','id');
    }
    public function region()
    {
        return $this->belongsTo('region','region_id','id');
    }
    /**
     * 内容管理列表
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     */
    public static function contentsList($page,$rows,$sort,$sortOrder,$searchName)
    {
        $result = self::with('region')->where(function($query)use($searchName){
            if($searchName){
                $query->where('project_id',$searchName);
            }
        })->order("{$sort}","{$sortOrder}")->page($page)->limit($rows)->select();
        $count = self::with('region')->where(function($query)use($searchName){
            if($searchName){
                $query->where('project_id',$searchName);
            }
        })->count();
        return json(['total'=>$count,'rows'=>$result]);
    }
    /**
     * 添加内容信息
     * @param Array $params 内容信息  project_id 项目id,high_score 最高分,low_score 最低分
     *                                sex 性别,score 分数,year年份，region_id 城市id
    */
    public static function contentsInsert($params)
    {
        return self::create($params);
    }
    /**
     * 删除内容信息
     * @param $id 内容信息id
    */
    public static function contentsDelete($id)
    {
        return self::destroy($id);
    }
    /**
     * 查询内容信息
     * @param $id 内容信息id
    */
    public static function contentsFind($id)
    {
        return self::where('id',$id)->find();
    }
    /**
     * 更新内容信息
     * @param Array $params 内容信息  project_id 项目id,high_score 最高分,low_score 最低分
     *                                sex 性别,score 分数,year年份，region_id 城市id
     */
    public static function contentsUpdate($id,$params)
    {
        return self::where('id',$id)->update($params);
    }
}